/*
 * ========================================================
 * Copyright(c) 2014 杭州龙骞科技-版权所有
 * ========================================================
 * 本软件由杭州龙骞科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * http://www.hzdracom.com/
 * ========================================================
 */
package com.shwread.httpsdk.http.face;

import android.content.Context;

import com.NewActionHelp;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.TrainActionHelp;
import com.shwread.httpsdk.constant.ResultCode;
import com.shwread.httpsdk.http.base.AbstractHttpPostDracom;
import com.shwread.httpsdk.http.callback.ActionListener;
import com.shwread.httpsdk.util.MD5;

import org.json.JSONObject;

import java.util.HashMap;

import logic.bean.LoginInfo;
import logic.constants.AppConfig;
import logic.constants.Const;


/**
 * @Project： Lqyd_Android
 * @Title：UserLoginAction
 * @Description：用户登录验证
 * @Author：wayne
 * @Date：2014年8月11日下午10:23:12
 */
public class UserLoginAction
        extends
        AbstractHttpPostDracom {

    private String password;
    private String account;

    /**
     * <p>
     * Title:
     * </p>
     * <p>
     * Description:
     * </p>
     *
     * @param context
     * @param action
     * @param actionListener
     */
    public UserLoginAction(Context context, String account, String password, ActionListener actionListener) {
        super(context, "userLogin.do", actionListener);
        this.password = password;
        this.account = account;
        Const.login_key = "";
    }

    @Override
    public void getParameter(HashMap<String, String> parameter_map) {
        parameter_map.put("account", account);
        parameter_map.put("password", MD5.MD5Encode(password));
        parameter_map.put("timeStamp", String.valueOf(timeStamp));
        parameter_map.put("enterpriseId", AppConfig.getEnterpriseId());
        parameter_map.put("cid", cid(account + Const.ACCOUNTTYPE + Const.CLIENTTYPE + timeStamp + MD5.MD5Encode(password)));
    }

    @Override
    public void doResponseXml(String xml) {
        println(xml);
        int result = -1;
        try {
            JSONObject jsonObject = new JSONObject(xml);
            result = jsonObject.getInt("resultCode");
            if (result == 1000) {
                String resultxml = jsonObject.getString("returnObject");
                Gson gson = new Gson();
                LoginInfo info = gson.fromJson(resultxml, new TypeToken<LoginInfo>() {
                }.getType());

                if (null != info) {
                    Const.phone_number = account;
                    Const.login_key = info.getKey();
                    NewActionHelp.setLoginKey(info.getKey());
                    NewActionHelp.setAccount(account);
                    TrainActionHelp.setLoginKey(info.getKey());
                    TrainActionHelp.setAccount(account);
                }

                listener.OK(info);
            } else if (result == 9102) //余额支付密码错3次
            {
                String resultxml = jsonObject.getString("returnObject");
                listener.ERROR(result, resultxml);
            } else {
                listener.ERROR(result, ResultCode.getResultText(result));
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.ERROR(result, "返回格式不正确");
        }

    }

    @Override
    protected void doRequestHeader(HashMap<String, String> header_map) {

    }

    @Override
    public void dispose() {

    }

}
